package com.lhd.shop.controller;

import com.alibaba.fastjson.JSON;
import com.lhd.shop.service.FrontService;
import com.lhd.shop.service.PayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vo.ResponseData;

import java.util.Map;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-12-03 15:19
 **/
@RestController
@Slf4j
@RequestMapping("/orderPay")
public class PayController {

    @Autowired
    private FrontService frontService;
    @Autowired
    private PayService payService;

    @PostMapping("/littlePay")
    public ResponseData<String> pay(@RequestBody Map<String, Object> map) {
        //map:{orderId=20191203152725724235024[订单号], discountedPrice=50.90[优惠金额], payPrice=130.10[支付金额], sourcePrice=181[原金额]}
        log.info("小程序付款参数:" + map.toString());
        String json = JSON.toJSONString(map);
        log.info("map转换为json：" + json);
        ResponseData<Boolean> updateVal = frontService.updateOrder(json);
        ResponseData<String> towCode = null;
        if (updateVal.getBody()) {
            towCode = payService.qrpay(map.get("orderId").toString());
        }
        return towCode;
    }

    @GetMapping("/queryPay")
    public ResponseData<Boolean> queryPay(String orderNo) {
        ResponseData<String> result = frontService.queryPay(orderNo);
        if (result.getStatusCode() == 200) {
            return new ResponseData<>(true);
        } else {
            return new ResponseData<>(false);
        }
    }

}
